<template>
  <div>
    <div>
      <el-row :gutter="15">
        <el-col :span="8">
          <div id="inputCheck"
               class="grid-content">
            <el-input placeholder="输入关键字"
                      v-model="input">
            </el-input>
          </div>
        </el-col>
        <el-col :span="2">
          <div class="grid-content ">
            <el-button class="check"
                       @click="clickByWord(input)">查询</el-button>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content "></div>
        </el-col>
        <el-col :span="2">
          <div class="grid-content ">
            <el-button class="new"
                       @click="dialogVisibleNew = true">新建</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-table :header-cell-style="{background:'#eef1f6',color:'#606266'}"
              :data="tableData"
              with
              style="width: 100%">
      <el-table-column prop="egId"
                       label="序号">
      </el-table-column>
      <el-table-column prop="name"
                       label="预案名称">
      </el-table-column>
      <el-table-column prop="principal"
                       label="负责人"
                       width="225">
      </el-table-column>
      <el-table-column prop="createTime"
                       label="创建时间">
      </el-table-column>
      <el-table-column prop="planRadio"
                       label="预案广播">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text"
                     @click="clickEdit (scope.row),dialogVisibleEdit= true">编辑</el-button>
          <el-button type="text"
                     @click="clickCheck(scope.row),dialogVisibleCheck= true">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--新建对话框-->
    <el-dialog custom-class="self-dialog"
               title="新建"
               :visible.sync="dialogVisibleNew"
               width="30%"
               modal>
      <el-form :model="formNew"
               :rules="rules"
               ref="formNew"
               label-width="100px">
        <el-form-item label="预案名称"
                      prop="planName">
          <el-input v-model="formNew.planName"
                    placeholder="单行输入"></el-input>
        </el-form-item>
        <el-form-item label="负责人"
                      prop="manager">
          <el-input v-model="formNew.manager"
                    placeholder="单行输入"></el-input>
        </el-form-item>
        <el-button @click="clickReset('formNew');dialogVisibleNew=false"
                   plain>取 消</el-button>
        <el-button @click="clickAddConfirm(formNew);dialogVisibleNew=false"
                   plain>确 定</el-button>

      </el-form>

    </el-dialog>
    <!-- 新建对话框 -->
    <!--编辑对话框-->
    <el-dialog custom-class="self-dialog"
               title="编辑"
               :visible.sync="dialogVisibleEdit"
               width="30%"
               modal>
      <el-form :model="formEdit"
               :rules="rules"
               ref="formEdit"
               label-width="100px">
        <el-form-item label="预案名称"
                      prop="planName">
          <el-input v-model="formEdit.planName"
                    placeholder="单行输入"></el-input>
        </el-form-item>
        <el-form-item label="负责人"
                      prop="manager">
          <el-input v-model="formEdit.manager"
                    placeholder="单行输入"></el-input>
        </el-form-item>
        <el-button @click="clickReset('formEdit');dialogVisibleEdit=false"
                   plain>取 消</el-button>
        <el-button @click="clickEditConfirm(formEdit);dialogVisibleEdit=false"
                   plain>确 定</el-button>

      </el-form>

    </el-dialog>
    <!-- 编辑对话框 -->
    <!--查看对话框-->
    <el-dialog custom-class="self-dialog"
               title="查看"
               :visible.sync="dialogVisibleCheck"
               width="30%"
               modal>
      <el-form :model="formCheck"
               :rules="rules"
               ref="formCheck"
               label-width="100px">
        <el-form-item label="预案名称"
                      prop="planName">
          <el-input v-model="formCheck.planName"
                    readonly></el-input>
        </el-form-item>
        <el-form-item label="负责人"
                      prop="manager">
          <el-input v-model="formCheck.manager"
                    readonly></el-input>
        </el-form-item>
        <el-form-item label="创建时间"
                      prop="createDate">
          <el-input v-model="formCheck.createDate"
                    readonly></el-input>
        </el-form-item>
        <el-button @click="clickReset('formCheck');dialogVisibleCheck=false"
                   plain>确定</el-button>
      </el-form>

    </el-dialog>
    <!-- 查看对话框 -->
  </div>
</template>
<script>
export default {
  data () {
    return {
      dialogVisibleNew: false,
      dialogVisibleEdit: false,
      dialogVisibleCheck: false,
      formNew: {
        planName: '',
        manager: ''
      },
      formEdit: {
        planName: '',
        manager: '',
        egId: ''
      },
      formCheck: {
        planName: '',
        manager: '',
        createDate: ''
      },
      tableData: [
        {
          egId: '1823',
          name: '',
          createTime: '',
          principal: ''
        },
      ],
      input: '',
      rules: {
        planName: [
          { required: true, message: '请输入预案名称', trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '请输入负责人姓名', trigger: 'blur' },
          { min: 2, max: 11, message: '长度为2-11个字符', trigger: 'blur' }
        ]

      }
    }
  },
  // 初始化显示所有用户
  created () {
    this.init()
  },
  methods: {
    // 显示所有用户
    init () {
      const _this = this
      this.$axios.get('/api/1/emergency/all').then(function (resp) {
        //console.log(resp);
        _this.tableData = resp.data.data
      })
    },
    // 根据关键字查询
    clickByWord (input) {
      const _this = this
      console.log(input)
      this.$axios.post(`/api/${this.scId}/emergency/findByWord`,
        {
          name: input,
        }

      ).then(function (resp) {
        if (resp.data.status === 200) {
          _this.tableData = resp.data.data
        }else {
          this.$message.warning("查询失败")
        }
      })

    },
    //查看
    clickCheck (row) {
      //console.log(row)
      this.formCheck.planName = row.name
      this.formCheck.manager = row.principal
      this.formCheck.createDate = row.createTime
    },

    // 增加预案
    clickAddConfirm (form) {
      console.log(form)
      const _this = this
      this.$axios.post('/api/1/emergency/add',
        // 法1: form
        //法2：
        {
          name: form.planName,
          principal: form.manager
        }


      ).then(function (resp) {
        console.log(resp)
        _this.init()
      })
    },
    // 编辑预案
    clickEdit (row) {
      //console.log(row.egId)
      this.formEdit.egId = row.egId
      this.formEdit.planName = row.name
      this.formEdit.manager = row.principal
      this.formEdit.createDate = row.createTime

    },
    // 编辑表单里的确定按钮
    clickEditConfirm (form) {
      //console.log(form)
      const _this = this
      this.$axios.post('/api/1/emergency/addById',
        // 法1：form
        //法2：
        {
          egId: form.egId,
          name: form.planName,
          principal: form.manager,
        }

      ).then(function () {
        // console.log(resp)
        _this.init()
      })
    },
    // 新增表单和编辑表单的取消操作
    clickReset (formName) {
      this.$refs[formName].resetFields()
    },
  },
  computed: {
    scId() {
      return this.$store.state.scId;
    },
  },
  watch: {
    scId: {
      handler() {
        this.init()
      },
      immediate: true,
      deep: true,
    },
  },

}
</script>
<style scoped>
#inputCheck {
  .el-input__inner {
    margin: 0;
    padding: 0;
    width: 400px;
  }
  .el-input--suffix,
  .el-input__inner {
    padding-right: 0px;
  }
  .el-input,
  .el-input--suffix {
    margin: 0;
    width: 40%;
    float: left;
    position: relative;
  }
  .input_ {
    position: relative;
    left: 0px;
  }
}

.el-table th.is-leaf {
  /* 去除上边框 */
  border: none;
}
.el-table::before {
  /* 去除下边框 */
  height: 0;
}
.el-table__row > td {
  /* 去除表格线 */
  border: none;
}
.el-table td,
.el-table th {
  text-align: center !important;
}
html,
body {
  margin: 0;
  padding: 0;
}
.check {
  float: left;
  position: relative;
}
.new {
  float: right;
  position: relative;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
